package org.xhnews.ui;

import java.util.Vector;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

import org.xhnews.bo.NewsBo;
import org.xhnews.to.NewsItem;

public class FavorListField extends ListField implements ListFieldCallback,ShowWindow{
	private Vector _newslist;
	Bitmap readPic = Bitmap.getBitmapResource("read.png");
	Bitmap favorPic = Bitmap.getBitmapResource("favor.png");
	Bitmap focusPic = Bitmap.getBitmapResource("focus.png");
	public FavorListField() {
		_newslist = new Vector();
		setRowHeight(getFont().getHeight()*2);
		setCallback(this);
		initialize();
	}

	public void initialize() {		
		reload();
	}
    
	public void reload() {
		Vector newsitems = NewsBo.getFavorNews();
		_newslist = newsitems;
		setSize(_newslist.size());
	}
	public String getSelectedNewsId(){
		return ((NewsItem)_newslist.elementAt(this.getSelectedIndex())).getID();		
	}
	public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
		NewsItem temp = (NewsItem) _newslist.elementAt(index);
		String title = temp.getTitle();
		String pubDate = temp.getPubDate();
		String source = temp.getChannelTitle();
		if (!temp.isRead())
			g.drawBitmap(0, y + 2, 3, list.getRowHeight() - 2, readPic, 0, 0);

		g.drawText(title, 4, y, 0, w);

		if (temp.isFavorite())
			g.drawBitmap(w - favorPic.getWidth(), y, favorPic.getWidth(),
					favorPic.getHeight(), favorPic, 0, 0);
        g.setColor(Color.GREEN);
		g.drawText(source, 4, y + g.getFont().getHeight() + 2);
		g.setColor(Color.GRAY);
		g.drawText(pubDate + " ", 4, y + g.getFont().getHeight() + 2,
				DrawStyle.RIGHT, w);
		g.drawLine(4, y + g.getFont().getHeight()*2-1, getWidth(), y + g.getFont().getHeight()*2-1);
		g.setColor(Color.BLACK);
	}

	public Object get(ListField list, int index) {
		return _newslist.elementAt(index);
	}

	public int indexOfList(ListField list, String prefix, int string) {
		return _newslist.indexOf(prefix, string);
	}

	public int getPreferredWidth(ListField list) {
		return Display.getWidth();
	}

	private void navigationToNewsViewScreen() {
		NewsItem newsitem = (NewsItem) _newslist.elementAt(getSelectedIndex());
		NewsBo.setItemRead(newsitem.getID());
		reload();
		String html = NewsBo.getNewsItemContent(newsitem.getID());
		NewsViewScreen screen = new NewsViewScreen();
		screen.initialize(html);
		
		UiApplication.getUiApplication().pushScreen(screen);
	}

	protected boolean navigationClick(int status, int time) {
		navigationToNewsViewScreen();
		return true;
	}

	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ENTER) {
			navigationToNewsViewScreen();
		}
		return true;
	}
	
}
